package u1project.server;

import u1project.common.Request;
import u1project.common.Response;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Method;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class ServerThread extends Thread {

    private final Socket s;

    public ServerThread(Socket s) {
        this.s = s;
    }

    public Response login(Request request) {
        //读取客户端提供的登录信息
        Response rep = null;
        String name = (String) request.get("name");
        String pass = (String) request.get("pass");
        if ("admin".equals(name) && "1234".equals(pass)) {
            rep = new Response(true);
        } else {
            rep = new Response(false, "登录失败，请核对！");
        }
        return rep;
    }

    //注册
    public Response register(Request request) {
        //读取客户端提供的登录信息
        Response rep = null;
        String name = (String) request.get("name");
        String pass = (String) request.get("pass");
        String pass2 = (String) request.get("repass");
        if ("admin".equals(name)) {
            rep = new Response(false, "用户已经存在！");
        } else if (!pass.equals(pass2)) {
            rep = new Response(false, "两次密码不一致！");
        } else if (pass.length() < 6) {
            rep = new Response(false, "密码不符合安全规则，太短了！");
        } else {
            rep = new Response(true, "注册成功");
        }
        return rep;
    }

    /**
     * 下载小说
     *
     * @param request
     * @return
     */
    public Response download(Request request) {
        Response rep = new Response(true);
        List<String> novels = new ArrayList<>();
        novels.add("倚天屠狗记");
        novels.add("105个男人和3个女人的故事");
        novels.add("大宋孽缘");
        novels.add("西游记");
        rep.put("novellist", novels);
        return rep;
    }

    @Override
    public void run() {
        try {
            ObjectInputStream oin = new ObjectInputStream(s.getInputStream());
            ObjectOutputStream oout = new ObjectOutputStream(s.getOutputStream());
            while (true) {
                Request request = (Request) oin.readObject();
                Response rep = null;
                //读到请求，开始处理
                try {
                    Method method = this.getClass().getDeclaredMethod(request.getAction(), Request.class);
                    rep = (Response) method.invoke(this, request);
                    if (request.getAction().equals("exit")) {
                        break;
                    }
                } catch (Exception ee) {
                    ee.printStackTrace();
                    rep = new Response(false, request.getAction() + "操作错误，原因是：" + ee);
                }
//                if (request.getAction().equals("login")) {
//                    rep = login(request);
//                } else if (request.getAction().equals("register")) {
//                    rep = register(request);
//                } else if (request.getAction().equals("download")) {
//                    rep = download(request);
//                } else {
//                    rep = new Response(false, "功能[" + request.getAction() + "]未实现");
//                }
                oout.writeObject(rep);
                oout.reset();//重置；否则，会发上次的对象
            }
        } catch (Exception e) {
            //            if(s.isConnected()){
            //链接还在
            ////                //有异常，发一下通知
            ////            }
            e.printStackTrace();

        } finally {
            try {
                s.close();
                System.out.println(s + "退出了。。。");
            } catch (Exception e) {

            }
        }
    }
}
